<?php
include_once ('../../Model/ConnectionDatabase.php');
class file_csv{
	private $connect;
	private $mysqli;
	private $db_name;
	private $header = null;
	private $footer = null;
	private $body = null;
	private $bottom = null;
	
	const SYSTEM_CODE = 'CSY-RVR-GWK52M78';
	private $fileName;
	function __construct(){		
		$this->connect = new ConnectionDatabase ();
		$this->mysqli = $this->connect->getMysqli ();
		$this->db_name = $this->connect->getDB_name ();
	}
	
	/**
	 * 
	 */	
	function headerCSV($date, $username){
		$this->header[0] = 'CSY-RVR-GWK52M78';
		$this->header[1] = substr($date, 6,4);
		$this->header[2] = substr($date, 3,2);
		//NGay thang tao file		
		$this->header[3] = date('Y');
		$this->header[4] = date('m');
		$this->header[5] = date('d');
		$this->header[6] = date('G');
		$this->header[7] = date('i');
		$this->header[8] = date('s');		
				
		$sql = "select a.* from ".$this->db_name.".account a where username='".$username."'";
		$result = $this->mysqli->query ($sql);
		
		while ($row = $result->fetch_array (MYSQLI_ASSOC)) {
			$this->header[9] = $row ['username'];
			$this->header[10] = ''.$row ['first_name'].' '.$row ['last_name'].'';
			
		}
		
		$this->footer[0] = 'END_____END_____END';
		$this->footer[1] = date('Y');
		$this->footer[2] = date('m');		
	}
	function writeCSV($date, $username){		
		//Lay ten file 01/01/2012		
		$fileName = 'RVR-'.substr($date, 6,4).'-'.substr($date, 3,2);		
		try{			
			/*Debug loi */			
			$file='../../../log/debug/'.date("Y-m-d").'.txt';
			$f=fopen($file,'a+');
			if($f){
				fwrite($f,"EXPORT_CSV_SUCCESS: Username: ".$username." File-name: ".$fileName.".csv Date-debug:".gmdate('Y-m-d H:i:s')."\r");
			}	
			fclose($f);
					
			$fp = fopen('../../../upload/csv/'.$fileName.'.csv', 'w');		
			$this->headerCSV($date, $username);
			//$csv = join("\t", $this->header)."\r\n";
			fputcsv($fp, $this->header);
			$sql = "select a.id,b.*, sum(total) sum_total from ".$this->db_name.".order a, ".$this->db_name.".account b where b.username=a.user_id and a.start_time like '%".substr($date, 3, 7)."%' group by a.user_id";		
			$result = $this->mysqli->query ($sql);
			$i=0;
			while ($row = $result->fetch_array (MYSQLI_ASSOC)) {
				$this->body[$i][0] = $row['username'];
				$this->body[$i][1] = ''.$row ['first_name'].' '.$row ['last_name'].'';
				$this->body[$i][2] = $row['sum_total'];
				$this->body[$i][3] = ''.$row['address'].'';
				$this->body[$i][4] = $row['mobi'];
				$i++;
			}
			if($this->body != null){
				foreach ($this->body as $fields) {
					//$fields = chr(255).chr(254).mb_convert_encoding($fields, "UTF-16LE", "UTF-8");
			    	fputcsv($fp, $fields);
			    	//$csv .= join("\t", $fields)."\r\n";
				}
			}
			//Footer
	//		$csv .= join("\t", $this->footer)."\r\n";
	//		$csv = chr(255).chr(254).mb_convert_encoding($csv, "UTF-16LE", "UTF-8");
	//		header("Content-type: application/x-msdownload");
	//		header("Content-disposition: csv; filename=".$fileName.".csv; size=".strlen($csv));
	//		
			fputcsv($fp, $this->footer);
			//$write = fwrite($fp, $csv);
			//echo $csv;
			
			fclose($fp);
			return $fileName.'.csv';
		}catch(Exception $e){
			$file='../../../log/error/'.date("Y-m-d").'.txt';
			$f=fopen($file,'a+');
			if($f){
				fwrite($f,"EXPORT_CSV_ERROR: Username: ".$username." Error: ".$e->getMessage().".csv Date-debug:".gmdate('Y-m-d H:i:s')."\r");
			}
			fclose($f);
		}
	}
}
$date = $_REQUEST['date'];
$username = $_REQUEST['username'];
$xx = new file_csv();
echo $xx->writeCSV($date, $username);



